{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {
                "azdata_cell_guid": "e4ed0892-7b5a-4d95-bd0d-a6c3eb0b2c99"
            },
            "source": [
                "![Microsoft](https://raw.githubusercontent.com/microsoft/azuredatastudio/main/extensions/arc/images/microsoft-small-logo.png)\n",
                " \n",
                "## Create SQL managed instance - Azure Arc on an existing Azure Arc Data Controller\n",
                " \n",
                "This notebook walks through the process of creating a <a href=\"https://docs.microsoft.com/azure/sql-database/sql-database-managed-instance\">SQL managed instance - Azure Arc</a> on an existing Azure Arc Data Controller.\n",
                " \n",
                "* Follow the instructions in the **Prerequisites** cell to install the tools if not already installed.\n",
                "* Make sure you have the target Azure Arc Data Controller already created.\n",
                "\n",
                "<span style=\"color:red\"><font size=\"3\">Please press the \"Run All\" button to run the notebook</font></span>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "azdata_cell_guid": "d1c8258e-9efd-4380-a48c-cd675423ed2f"
            },
            "source": [
                "### **Prerequisites** \n",
                "Ensure the following tools are installed and added to PATH before proceeding.\n",
                " \n",
                "|Tools|Description|Installation|\n",
                "|---|---|---|\n",
                "|Azure CLI (az) | Command-line tool for installing and managing resources in an Azure Arc cluster |[Installation](https://docs.microsoft.com/cli/azure/install-azure-cli-windows?tabs=azure-cli) |\n",
                "|Azure CLI arcdata extension | Commands for using Azure Arc for Azure data services. | [Installation](https://docs.microsoft.com/azure/azure-arc/data/install-arcdata-extension)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "azdata_cell_guid": "68531b91-ddce-47d7-a1d8-2ddc3d17f3e7"
            },
            "source": [
                "### **Setup and Check Prerequisites**"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {
                "azdata_cell_guid": "749d8dba-3da8-46e9-ae48-2b38056ab7a2",
                "tags": []
            },
            "outputs": [],
            "source": [
                "import sys,os,json,subprocess\n",
                "def run_command():\n",
                "    print(\"Executing: \" + cmd)\n",
                "    output = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )\n",
                "    if output.returncode != 0:\n",
                "        print(f'Command: {cmd} failed \\n')\n",
                "        print(f'\\t>>>Error output: {output.stderr.decode(\"utf-8\")}\\n')\n",
                "        sys.exit(f'exit code: {output.returncode}\\n')\n",
                "    print(f'Successfully executed: {cmd}')\n",
                "    print(f'\\t>>>Output: {output.stdout.decode(\"utf-8\")}\\n')\n",
                "    return output.stdout.decode(\"utf-8\")\n",
                "cmd = 'az --version'\n",
                "out = run_command()\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "azdata_cell_guid": "68ec0760-27d1-4ded-9a9f-89077c40b8bb"
            },
            "source": [
                "### **Set variables**\n",
                "\n",
                "#### \n",
                "\n",
                "Generated by Azure Data Studio using the values collected in the 'Deploy Azure SQL managed instance - Azure Arc' wizard"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "azdata_cell_guid": "90b0e162-2987-463f-9ce6-12dda1267189"
            },
            "source": [
                "### **Creating the SQL managed instance - Azure Arc instance**"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {
                "azdata_cell_guid": "4fbaf071-55a1-40bc-be7e-7b9b5547b886"
            },
            "outputs": [],
            "source": [
                "print (f'Creating the SQL managed instance - Azure Arc instance')\n",
                "\n",
                "is_indirect = arc_data_controller_connectivity_mode.lower() == 'indirect'\n",
                "is_general_purpose = sql_service_tier == 'GeneralPurpose'\n",
                "\n",
                "# Indirect Mode Parameters\n",
                "retention_days = f' --retention-days \"{sql_retention_days}\"' if is_indirect and sql_retention_days else \"\"\n",
                "\n",
                "# General Parameters\n",
                "use_k8s = ' --use-k8s'\n",
                "namespace = f' --k8s-namespace {arc_data_controller_namespace}' if arc_data_controller_namespace else \"\"\n",
                "cores_request_option = f' --cores-request \"{sql_cores_request}\"' if sql_cores_request else \"\"\n",
                "cores_limit_option = f' --cores-limit \"{sql_cores_limit}\"' if sql_cores_limit else \"\"\n",
                "memory_request_option = f' --memory-request \"{sql_memory_request}Gi\"' if sql_memory_request else \"\"\n",
                "memory_limit_option = f' --memory-limit \"{sql_memory_limit}Gi\"' if sql_memory_limit else \"\"\n",
                "\n",
                "sql_replicas_option = f' --replicas {sql_replicas}' if sql_replicas and not is_general_purpose else \"\"\n",
                "readable_secondaries = f' --readable-secondaries \"{sql_readable_secondaries}\"' if sql_readable_secondaries and not is_general_purpose else \"\"\n",
                "sync_secondary_to_commit = f' --sync-secondary-to-commit \"{sql_sync_secondary_to_commit}\"' if sql_sync_secondary_to_commit and not is_general_purpose else \"\"\n",
                "\n",
                "\n",
                "storage_class_data_option =  f' --storage-class-data \"{sql_storage_class_data}\"'if sql_storage_class_data else \"\"\n",
                "storage_class_datalogs_option =  f' --storage-class-datalogs \"{sql_storage_class_datalogs}\"'if sql_storage_class_datalogs else \"\"\n",
                "storage_class_logs_option =  f' --storage-class-logs \"{sql_storage_class_logs}\"'if sql_storage_class_logs else \"\"\n",
                "storage_class_backup_option = f' --storage-class-backups \"{sql_storage_class_backups}\"'if sql_storage_class_backups else \"\"\n",
                "\n",
                "volume_size_data = f' --volume-size-data {sql_volume_size_data}Gi'\n",
                "volume_size_datalogs = f' --volume-size-datalogs {sql_volume_size_datalogs}Gi'\n",
                "volume_size_logs = f' --volume-size-logs {sql_volume_size_logs}Gi'\n",
                "volume_size_backups = f' --volume-size-backups {sql_volume_size_backups}Gi'\n",
                "\n",
                "service_tier = f' --tier {sql_service_tier}'\n",
                "cores_limit = f' --cores-limit {sql_cores_limit}'\n",
                "dev_use = ' --dev' if sql_dev_use == 'true' else ''\n",
                "license_type = ' --license-type BasePrice' if sql_license_type else ' --license-type LicenseIncluded'\n",
                "\n",
                "os.environ[\"AZDATA_USERNAME\"] = sql_username\n",
                "os.environ[\"AZDATA_PASSWORD\"] = os.environ[\"AZDATA_NB_VAR_SQL_PASSWORD\"]\n",
                "cmd = f'az sql mi-arc create --name {sql_instance_name}{namespace}{sql_replicas_option}{cores_request_option}{cores_limit_option}{memory_request_option}{memory_limit_option}{readable_secondaries}{sync_secondary_to_commit}{storage_class_data_option}{storage_class_datalogs_option}{storage_class_logs_option}{storage_class_backup_option}{volume_size_data}{volume_size_datalogs}{volume_size_logs}{volume_size_backups}{retention_days}{service_tier}{dev_use}{license_type}{cores_limit}{use_k8s}'\n",
                "out=run_command()"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.6.6"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
